package com.xm.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xm.common.utils.PagedResult;
import com.xm.common.utils.TimeAgoUtils;
import com.xm.common.utils.idworker.Sid;
import com.xm.mapper.*;
import com.xm.pojo.Comments;
import com.xm.pojo.SearchRecords;
import com.xm.pojo.UsersLikeVideos;
import com.xm.pojo.Videos;
import com.xm.service.VideoService;
import com.xm.vo.CommentsVo;
import com.xm.vo.VideosVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideosMapper videosMapper;

    @Autowired
    private VideosCustomMapper videosCustomMapper;

    @Autowired
    private SearchRecordsMapper searchRecordsMapper;

    @Autowired
    private UsersLikeVideosMapper usersLikeVideosMapper;

    @Autowired
    private UsersCustomMapper usersCustomMapper;

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private CommentsCustomMapper commentsCustomMapper;

    @Autowired
    private Sid sid;



    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void userLikeVideo(String userId, String videoId, String videoOwnId) {
        //对关联表进行插入操作
        UsersLikeVideos usersLikeVideos = new UsersLikeVideos();
        usersLikeVideos.setId(sid.nextShort());
        usersLikeVideos.setUserId(userId);
        usersLikeVideos.setVideoId(videoId);
        usersLikeVideosMapper.insert(usersLikeVideos);
        //对用户表操作
        usersCustomMapper.addUserLikeCount(userId);
        //对视频表操作
        videosCustomMapper.addVideoLikeCount(videoId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void userUnLikeVideo(String userId, String videoId, String videoOwnId) {
        //对关联表进行删除操作
        Example example = new Example(UsersLikeVideos.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("videoId",videoId);
        usersLikeVideosMapper.deleteByExample(example);

        //对用户表操作
        usersCustomMapper.reduceUserLikeCount(userId);
        //对视频表操作
        videosCustomMapper.reduceVideoLikeCount(videoId);
    }





    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateVideo(String videoId, String coverPath) {
        Videos video = new Videos();
        video.setId(videoId);
        video.setCoverPath(coverPath);
        videosMapper.updateByPrimaryKeySelective(video);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult getFollowVideos(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<VideosVo> videosVos = videosCustomMapper.queryFollowVideos(userId);

        PageInfo<VideosVo> pageList = new PageInfo<>(videosVos);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRecords(pageList.getTotal());
        pagedResult.setRows(videosVos);
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult getAllComments(String videoId, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        List<CommentsVo> commentsVos = commentsCustomMapper.queryComments(videoId);

        //对评论显示的时间进行设置
        for(CommentsVo commentsVo: commentsVos){
            String timeAgo = TimeAgoUtils.format(commentsVo.getCreateTime());
            commentsVo.setTimeAgoStr(timeAgo);
        }

        PageInfo<CommentsVo> pageList = new PageInfo<>(commentsVos);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRecords(pageList.getTotal());
        pagedResult.setRows(commentsVos);
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult getLikeVideos(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<VideosVo> videosVos = videosCustomMapper.queryLikeVideos(userId);
        PageInfo<VideosVo> pageList = new PageInfo<>(videosVos);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRecords(pageList.getTotal());
        pagedResult.setRows(videosVos);
        return pagedResult;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveComments(Comments comments) {
        String cId = sid.nextShort();
        comments.setId(cId);
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PagedResult getAllVideos(Videos video,Integer isSavedRecord,Integer page, Integer pageSize) {

        String desc = video.getVideoDesc();
        String userId = video.getUserId();
        if(isSavedRecord == 1){
            SearchRecords searchRecords = new SearchRecords();
            String id = sid.nextShort();
            searchRecords.setId(id);
            searchRecords.setContent(desc);
            searchRecordsMapper.insert(searchRecords);
        }

        PageHelper.startPage(page,pageSize);

        List<VideosVo> videosVos = videosCustomMapper.queryAllVideos(desc,userId);
        PageInfo<VideosVo> pageList = new PageInfo<>(videosVos);

        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRecords(pageList.getTotal());
        pagedResult.setRows(videosVos);


        return pagedResult;
    }

    @Override
    public List<String> getHotWords() {
        return searchRecordsMapper.getHotWords();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public String saveVideo(Videos video) {
        String videoid = sid.nextShort();
        video.setId(videoid);
        //对于null会使用数据库默认值
        videosMapper.insertSelective(video);

        return videoid;
    }
}
